package org.gowk.toolkit.expdbdoc.doc;

import java.io.FileOutputStream;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGrid;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;

import com.jfinal.plugin.activerecord.Record;

/**
 * word操作
 * 
 * @author hxl
 * @date 2012-6-27下午10:26:12
 */
public final class ExpDic {

    /**
     * 生成word，表格
     * 
     * @param data
     * @throws Exception
     */
    public static void productWordForm(Map<String, HashMap<String, LinkedHashMap<String, String>>> data,
                                       Parameters parameters) throws Exception {
        XWPFDocument xDocument = new XWPFDocument();
        DBUtils dbUtils=new DBUtils();

        Iterator<String> tableNameIter = data.keySet().iterator();
        while (tableNameIter.hasNext()) {
            String table_name = tableNameIter.next();
            XWPFParagraph xp = xDocument.createParagraph();
            XWPFRun r1 = xp.createRun();
            r1.setText(table_name);
            r1.setFontSize(18);
            r1.setTextPosition(10);

            HashMap<String, LinkedHashMap<String, String>> columns = data.get(table_name);
            
            table_name=table_name.split("\\(")[0];
            System.out.println("--table_name-"+table_name);
			List<Record> records = dbUtils.getDicInfo(table_name);
			System.out.println("--size--"+records.size());
			int rows = records.size();
            XWPFTable xTable = xDocument.createTable(rows + 2, 8);
            
          //设置表格宽度
			CTTbl ttbl = xTable.getCTTbl();
			CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();
			CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();
			CTJc cTJc = tblPr.addNewJc();
			cTJc.setVal(STJc.Enum.forString("center"));
			tblWidth.setW(new BigInteger("8000"));
			tblWidth.setType(STTblWidth.DXA);

            //xTable.setWidth(10240);
			
			CTTblGrid tblGrid = ttbl.getTblGrid() != null ? ttbl.getTblGrid() : ttbl.addNewTblGrid();
			tblGrid.addNewGridCol().setW(new BigInteger("1000"));
			tblGrid.addNewGridCol().setW(new BigInteger("1400"));
			tblGrid.addNewGridCol().setW(new BigInteger("600"));
			tblGrid.addNewGridCol().setW(new BigInteger("1000"));
			tblGrid.addNewGridCol().setW(new BigInteger("1000"));
			tblGrid.addNewGridCol().setW(new BigInteger("1000"));
			tblGrid.addNewGridCol().setW(new BigInteger("1000"));
			tblGrid.addNewGridCol().setW(new BigInteger("1000"));
			
			
            int i = 0;
            
            for(int j=0;j<8;j++){
            	XWPFParagraph p = xDocument.createParagraph();
            	p.setAlignment(ParagraphAlignment.CENTER);
            	xTable.getRow(i).getCell(j).setParagraph(p);
            	xTable.getRow(i).getCell(j).setColor("C0C2CC");
            }

            xTable.getRow(i).getCell(0).setText("字典编号");
            xTable.getRow(i).getCell(1).setText("字典名称");
            xTable.getRow(i).getCell(2).setText("地址");
            xTable.getRow(i).getCell(3).setText("父编号");
            xTable.getRow(i).getCell(4).setText("是否最末级节点");
            xTable.getRow(i).getCell(5).setText("排序编号");
            xTable.getRow(i).getCell(6).setText("层级");
            xTable.getRow(i).getCell(7).setText("层级路径");
            

			for (int j = 0; j < records.size(); j++) {
				i++;
				Record record = records.get(j);
				//System.out.println(i+"--record--"+record);
				//String tid=record.getStr("tid")!=null?record.getStr("tid"):record.getStr("orgid");
				//String tname=record.getStr("tname")!=null?record.getStr("tname"):record.getStr("orgname");
				//String turl=record.getStr("turl")!=null?record.getStr("turl"):"";
				xTable.getRow(i).getCell(0).setText(record.getStr("TID"));
				xTable.getRow(i).getCell(1).setText(record.getStr("TName"));
				xTable.getRow(i).getCell(2).setText(record.getStr("TURL"));
				xTable.getRow(i).getCell(3).setText(record.getStr("ParentID"));
				xTable.getRow(i).getCell(4).setText(record.getStr("IsLeaf"));
				xTable.getRow(i).getCell(5).setText(record.getNumber("SortID")+"");
				xTable.getRow(i).getCell(6).setText(record.getInt("TreeLevel")+"");
				xTable.getRow(i).getCell(7).setText(record.getStr("TreePath"));
			}

        }

        FileOutputStream fos = new FileOutputStream(parameters.getPath() + parameters.getDatabase() + "_doc.docx");

        xDocument.write(fos);
        fos.close();
    }

    /**
     * 检索出主键key相关的列
     * 
     * @param columnsMap
     * @return
     */
    private static Map<String, LinkedHashMap<String, String>> keyColumns(HashMap<String, LinkedHashMap<String, String>> columnsMap) {
        Map<String, LinkedHashMap<String, String>> keyColumnMap = new HashMap<String, LinkedHashMap<String, String>>();

        Iterator<String> cloumnsNameIter = columnsMap.keySet().iterator();
        while (cloumnsNameIter.hasNext()) {
            String colum_name = cloumnsNameIter.next();
            LinkedHashMap<String, String> columnsAtt = columnsMap.get(colum_name);
            if (columnsAtt.get("column_key").equals("是")) {
                keyColumnMap.put(colum_name, columnsAtt);
                cloumnsNameIter.remove();
            }
        }

        return keyColumnMap;
    }
}
